package com.samphanie.auiu.demo.banner

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import com.bumptech.glide.Glide
import com.samphanie.auiu.R
import com.samphanie.auiu.library.log.AuiuLog
import com.samphanie.auiu.ui.banner.AuiuBanner
import com.samphanie.auiu.ui.banner.core.AuiuBannerMo
import com.samphanie.auiu.ui.banner.indicator.AuiuCircleIndicator
import com.samphanie.auiu.ui.banner.indicator.AuiuIndicator
import com.samphanie.auiu.ui.banner.indicator.AuiuNumIndicator

class AuiuBannerDemoActivity : AppCompatActivity() {

    private val urls = arrayOf(
        "https://images.myxiaxiang.com/20210308/56ab173c97644129813ba7e803774a60.jpg",
        "https://images.myxiaxiang.com/20210308/b015b15f26394f3d9855b3735be0e885.jpg",
        "https://images.myxiaxiang.com/20210308/52c028cb33e0426cbacfe85e35c34b20.jpg",
        "https://img.zcool.cn/community/01b6685b38820fa80120b959b5f1cf.jpg@1280w_1l_2o_100sh.jpg",
        "https://img.zcool.cn/community/0117266065c46d11013e87f4bcbf9b.jpg@1280w_1l_0o_100sh.jpg"
    )

    private var autoPlay: Boolean = false
    private var auiuIndicator: AuiuIndicator<*>? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_auiu_banner_demo)

        initView(AuiuCircleIndicator(this), autoPlay)

        findViewById<Switch>(R.id.auto_play).setOnCheckedChangeListener { _, isChecked ->
            autoPlay = isChecked
            initView(auiuIndicator, autoPlay)
            AuiuLog.d("------自动播放: $autoPlay")
        }

        findViewById<View>(R.id.tv_switch).setOnClickListener {
            if (auiuIndicator is AuiuCircleIndicator) {
                initView(AuiuNumIndicator(this), autoPlay)
            } else {
                initView(AuiuCircleIndicator(this), autoPlay)
            }
        }
    }

    private fun initView(auiuIndicator: AuiuIndicator<*>?, autoPlay: Boolean) {
        this.auiuIndicator = auiuIndicator
        val mAuiuBanner = findViewById<AuiuBanner>(R.id.banner)
        val moList: MutableList<AuiuBannerMo> = ArrayList()

        for (index in urls.indices) {
            val mo = BannerMo()
            mo.url = urls[index]
            moList.add(mo)
        }

        mAuiuBanner!!.setAuiuIndicator(auiuIndicator)
        mAuiuBanner.setAutoPlay(autoPlay)
        mAuiuBanner.setIntervalTime(2000)
        // 自定义布局
        mAuiuBanner.setBannerData(R.layout.banner_item_layout, moList)
        mAuiuBanner.setBindAdapter {viewHolder, mo, position ->
            val imageView: ImageView = viewHolder.findViewById(R.id.iv_image)
            Glide.with(this@AuiuBannerDemoActivity).load(mo.url).into(imageView)
            val titleView: TextView = viewHolder.findViewById(R.id.tv_title)
            titleView.text = mo.url
            AuiuLog.d("------position:", position.toString() + "url:" + mo.url)
        }
    }
}